package com.spring.boot.rpc.rpc01;

import com.spring.boot.rpc.rpc00.IUserService;
import com.spring.boot.rpc.rpc00.User;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * 最原始的 rpc 通讯
 * 缺点：
 *  1、不灵活，业务代码混在了一起，不方便修改
 */
public class Server {
    private static boolean running = true;

    public static void main(String[] args) throws Exception {
        ServerSocket ss = new ServerSocket(8888);
        while (running){
            Socket s = ss.accept();
            process(s);
            s.close();
        }
        ss.close();


    }

    private static void process(Socket s) throws IOException {
        InputStream in = s.getInputStream();
        OutputStream out = s.getOutputStream();
        DataInputStream dis = new DataInputStream(in);
        DataOutputStream dos = new DataOutputStream(out);


        int id = dis.readInt();// 读到客户端发的4字节的数组
        IUserService service = new IUserServiceImpl();
        User user = service.findUserById(id);
        dos.writeInt(user.getId());// 传不了对象，就把对象的属性全传过去
        dos.writeUTF(user.getName());// 传不了对象，就把对象的属性全传过去
        dos.flush();
    }




}
